package yz.dut.edu.graduate.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yz.dut.edu.graduate.bean.Result;
import yz.dut.edu.graduate.pojo.Dish;
import yz.dut.edu.graduate.service.ComprehensiveService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/*
管理员操作合集
*/


@RestController
@RequestMapping("/dish")
public class DishAdminController {

    @Autowired
    ComprehensiveService comprehensiveService;

    @PostMapping("/insert")
    public Result DishInsert(@RequestBody Map<String,Object> map){
        String dishName=(String)map.get("dishname");
        int rec=(int)map.get("price");
        float price=(float)rec;
        int dishType=Integer.parseInt((String)map.get("type"));
        String ingredients=(String)map.get("ingredients");
        Object photo=map.get("photo");
        String [] ingredient =ingredients.split(" ");
        Dish dish=new Dish();
        dish.setDishName(dishName);
        dish.setPrice(price);
        dish.setState(0);
        dish.setDishType(dishType);
        Result result = comprehensiveService.DishAndIngredientInsert(dish,ingredient);
        return result;
    }


    @PostMapping("/delete")
    public Result DishDelete(@RequestBody Map<String,Object> map){
        String dishName=(String)map.get("dishName");
        Result result =comprehensiveService.DishAndIngredientDelete(dishName);
        return result;
    }

    @GetMapping("/show")
    public List<Dish> Show(){
        return comprehensiveService.SelectAll();
    }


    @GetMapping("/showPage")
    public PageInfo<Dish> ShowPage(HttpServletRequest request){
        int pageNum= Integer.parseInt(request.getParameter("pagenum"));
        int pageSize=Integer.parseInt(request.getParameter("pagesize"));
        return comprehensiveService.SelectPage(pageNum,pageSize);
    }

    @PostMapping("/deleteTable")
    public Result DishInTableDelete(HttpServletRequest request){
        String dishName=request.getParameter("dishname");
        return comprehensiveService.DishAndIngredientDelete(dishName);
    }

    @PostMapping("/searchDish")
    public List<Dish> DishSelect(HttpServletRequest request){
        String dishName=request.getParameter("dishname");
        return comprehensiveService.SelectCertain(dishName);
    }

    @PostMapping("/deleteTables")
    public Result DishInTablesDelete(HttpServletRequest request){
        String temp=request.getParameter("dishnames");
        String[] dishNames =temp.split(",");
        for(String dishName:dishNames) {
            comprehensiveService.DishAndIngredientDelete(dishName);
        }
        return new Result();
    }


    @RequestMapping("/off")
    public Result DishOff(@RequestBody Map<String,Object> map){
        String dishName=(String)map.get("dishName");
        int temp=(int)map.get("discount");
        float off=(float)temp;
        off/=100;
        Result result = comprehensiveService.DishPriceOff(dishName,off);
        return result;
    }

    @PostMapping("/update")
    public Result UpdatePriceAndOff(@RequestBody Map<String,Object> map){
        String dishName=(String)map.get("dishname");
        int price=(Integer)map.get("price");
        int state=Integer.parseInt((String) map.get("state"));
        return comprehensiveService.DishPriceUpdate(dishName,price,state);
    }

}
